package com.example.app.ui.Discover;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.app.R;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MentionActivity extends AppCompatActivity {
    private EditText etSearch;
    private RecyclerView rvContacts;
    private MentionAdapter adapter;
    private final List<String> selectedUsers = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mention);
        setupToolbar();
        setupSearch();
        setupContactsList();
    }
    private void setupToolbar() {
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("提醒谁看");
    }
    private void setupSearch() {
        etSearch = findViewById(R.id.et_search);
        etSearch.setHint("搜索");
        etSearch.addTextChangedListener(new TextWatcher() {
            @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                adapter.filter(s.toString());
            }
        });
    }
    private void setupContactsList() {
        rvContacts = findViewById(R.id.rv_contacts);
        rvContacts.setLayoutManager(new LinearLayoutManager(this));
        List<String> contacts = Arrays.asList("张三", "李四", "王五", "赵六");
        adapter = new MentionAdapter(contacts);
        rvContacts.setAdapter(adapter);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_messages, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.menu_done) {
            returnResult();
            return true;
        } else if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    private void returnResult() {
        Intent result = new Intent();
        result.putStringArrayListExtra("mentions", new ArrayList<>(selectedUsers));
        setResult(RESULT_OK, result);
        finish();
    }
    private class MentionAdapter extends RecyclerView.Adapter<MentionViewHolder> {
        private List<String> originalList;
        private List<String> filteredList;
        MentionAdapter(List<String> contacts) {
            this.originalList = contacts;
            this.filteredList = new ArrayList<>(contacts);
        }
        @SuppressLint("NotifyDataSetChanged")
        void filter(String query) {
            filteredList.clear();
            if (TextUtils.isEmpty(query)) {
                filteredList.addAll(originalList);
            } else {
                for (String contact : originalList) {
                    if (contact.toLowerCase().contains(query.toLowerCase())) {
                        filteredList.add(contact);
                    }
                }
            }
            notifyDataSetChanged();
        }
        @Override
        public MentionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_mention, parent, false);
            return new MentionViewHolder(view);
        }
        @Override
        public void onBindViewHolder(MentionViewHolder holder, int position) {
            String user = filteredList.get(position);
            holder.tvName.setText(user);
            holder.cbSelect.setChecked(selectedUsers.contains(user));

            holder.itemView.setOnClickListener(v -> {
                if (selectedUsers.contains(user)) {
                    selectedUsers.remove(user);
                } else {
                    selectedUsers.add(user);
                }
                notifyItemChanged(position);
            });
        }
        @Override
        public int getItemCount() {
            return filteredList.size();
        }
    }
    static class MentionViewHolder extends RecyclerView.ViewHolder {
        CheckBox cbSelect;
        TextView tvName;
        MentionViewHolder(View itemView) {
            super(itemView);
            cbSelect = itemView.findViewById(R.id.cb_select);
            tvName = itemView.findViewById(R.id.tv_name);
        }
    }
}